﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace DRHotel_v1.Classes.DBManagers
{
    class AccountCreationDBManager
    {

        public bool CreateAccount(string staffID, string username, string password, string usertype)
        {
            //bool flag = accountExist(staffID);
            bool creationStatus = false;
            //if (flag == true)
            //{
            SqlConnection conn = null;
            SqlCommand cmd = null;

            try
            {

                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.
                ConnectionStrings["DRHotel_v1.Properties.Settings.DRHotelMngSysConnectionString"].ConnectionString;
                conn.Open();

                cmd = new SqlCommand();
                cmd.CommandText = "Insert INTO Account (staffID,username,password,userType) VALUES (@staffID, @username, @password, @userType)";
                cmd.Connection = conn;

                cmd.Parameters.AddWithValue("@staffID", Int32.Parse(staffID));
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                cmd.Parameters.AddWithValue("@userType", usertype);
                int rows = cmd.ExecuteNonQuery();
                if (rows >= 1)
                {
                    creationStatus = true;

                }
                else
                {
                    creationStatus = false;
                }
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return creationStatus;
            //}
            /*else
            {
                return creationStatus;
            }*/
        }

        public static ArrayList PopulateStaff()
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader PopulateStaffDr = null;

            ArrayList staffDetails = new ArrayList();

            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.
                ConnectionStrings["DRHotel_v1.Properties.Settings.DRHotelMngSysConnectionString"].ConnectionString;
                conn.Open();

                cmd = new SqlCommand();
                cmd.CommandText = "Select * From Staff";
                cmd.Connection = conn;

                PopulateStaffDr = cmd.ExecuteReader();
                while (PopulateStaffDr.Read())
                {
                    StaffDetailClass sta = new StaffDetailClass(Convert.ToInt32(PopulateStaffDr["staffID"]),
                                               PopulateStaffDr["firstName"].ToString(),
                                               PopulateStaffDr["lastName"].ToString()
                                               );

                    staffDetails.Add(sta);

                }



            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                PopulateStaffDr.Close();
            }
            return staffDetails;
        }

        public bool AccountExist(string staffID, string username)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            bool flag = false;

            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.
                ConnectionStrings["DRHotel_v1.Properties.Settings.DRHotelMngSysConnectionString"].ConnectionString;
                conn.Open();

                cmd = new SqlCommand();
                cmd.CommandText = "Select staffID, username FROM Account;";
                cmd.Connection = conn;

                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    int retrieveStaffId = Convert.ToInt32(dr["staffID"]);
                    string retrieveUsername = dr["username"].ToString();
                    int inputStaffId = Convert.ToInt32(staffID);
                    string inputUsername = username;

                    if (inputStaffId == retrieveStaffId || inputUsername == retrieveUsername)
                    {
                        flag = true;
                        break;
                    }
                    else
                        flag = false;
                }
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
                dr.Close();
            }
            return flag;
        }

    }
}
